package com.linwen.javase.juc;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName _18VolatileDemo
 * @Description
 * @Author lin
 * @Date 2022/4/25 上午12:56
 * @Version V1.0
 *
 * volatile关键字可以保证内存可见行，即多个线程共同操作一个数据，数据的更新其余线程都可见
 *
 */
public class _18VolatileDemo {
    public static void main(String[] args) {
        VolatileTest volatileTest = new VolatileTest();
        new Thread(volatileTest).start();

        while (true){
            if(!volatileTest.isFalg()){
                System.out.println("flag = "+volatileTest.isFalg());
                break;
            }
        }

    }
}
class VolatileTest implements Runnable{
    private volatile boolean falg = true;

    public boolean isFalg() {
        return falg;
    }

    public void setFalg(boolean falg) {
        this.falg = falg;
    }

    @Override
    public void run() {
        try {
            TimeUnit.SECONDS.sleep(2);
            falg = false;
            System.out.println("flag = "+isFalg());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
